//
// Created by Administrator on 2020/7/11.
//T6章:数组、指针与字符串(2)
//动态内存分配
//1:动态分配与释放内存
//>动态申请内存操作符new
//new 类型名T(初始化参数列表)
//功能:在程序执行期间,申请用于存放T类型对象的内存空间,并依初始列表赋于初值
//结果:T类型的指针,指向新分配的内存;失败:抛出异常。
//>释放内存操作符delete
//delete 指针p
//功能:释放指针p所指向的内存。p必须是new操作的返回值


//eg:动态创建对象
#include <iostream>
using namespace std;
class Point
{
public:
    Point():x(0),y(0)
    {
        cout<<"Default Constructor called."<<endl;
    }
    Point(int x,int y):x(x),y(y){cout<<"Constructor called."<<endl;}
    ~Point(){cout<<"Destructor called."<<endl;}
    int getX()const{return x;}
    int getY()const{return y;}
    void move(int newX,int newY)
    {
        x=newX;
        y=newY;
    }
private:
    int x,y;
};
int main()
{
    cout<<"Step one:"<<endl;
    Point *ptr1=new Point;//调用默认构造函数
    //.....
    delete ptr1;//删除对象，自动调用析构函数

    cout<<"Step two:"<<endl;
    ptr1=new Point(1,2);
    //.....
    delete ptr1;

    return 0;
}